﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

using System.Configuration;
using System.Web.Security;
using SubSonic;

public partial class User_Employer_DeactiveUser : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
        Lblstatus.Text = "";
        var thisUser = HttpContext.Current.User;
        if (thisUser.Identity.IsAuthenticated)
        {
            if (thisUser.IsInRole("edituser"))
            {
                RadioButtonListRole.Items[2].Enabled = false;
            }
            else
            {
                RadioButtonListRole.Items[2].Enabled = true;
            }
        }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            BindingGrid();
        }
        catch
        { }
    }

    private void BindingGrid()
    {
        string orderExpression = DataAccess.TblUserDetail.Columns.Name;
        SubSonic.Query tempQuery = new Query(DataAccess.TblUserDetail.Schema);
        tempQuery.OrderBy = SubSonic.OrderBy.Asc("Name");
        if (ViewState["SortExpression"] != null && !ViewState["SortExpression"].ToString().Trim().Equals(String.Empty))
        {
            switch (ViewState["SortExpression"].ToString())
            {
                case "Name":
                    orderExpression = DataAccess.TblUserDetail.Columns.Name;
                    break;
                case "Family":
                    orderExpression = DataAccess.TblUserDetail.Columns.Family;
                    break;
                case "UserName":
                    orderExpression = DataAccess.TblUserDetail.Columns.UserName;
                    break;
            }

            tempQuery.OrderBy = ViewState["SortDirection"] != "Ascending" ? SubSonic.OrderBy.Desc(orderExpression) : SubSonic.OrderBy.Asc(orderExpression);
        }
        if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        {
            SubSonic.Query q1 = DataAccess.TblUserDetail.CreateQuery();
            q1.QueryType = SubSonic.QueryType.Select;
            q1.OrderBy = tempQuery.OrderBy;
            q1.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
            q1.AND(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhoghooghi");
            q1.OR(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhaghighi");

            DataSet dt1 = q1.ExecuteDataSet();
            if (dt1.Tables[0].Rows.Count > 0)
            {
                GridViewUsers.DataSource = dt1;
                GridViewUsers.DataBind();
            }
            else
            {
                Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                cleargrid();
            }
        }
        if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        {
            if (RadioButtonListAdvisors.SelectedItem.Value == "Advisorhoghooghi")
            {
                SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                q.QueryType = SubSonic.QueryType.Select;
                q.OrderBy = tempQuery.OrderBy;
                q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhoghooghi");
                DataSet dt = q.ExecuteDataSet();
                if (dt.Tables[0].Rows.Count > 0)
                {
                    GridViewUsers.DataSource = dt;
                    GridViewUsers.DataBind();
                }
                else
                {
                    Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                    cleargrid();
                }
            }
            if (RadioButtonListAdvisors.SelectedItem.Value == "Advisorhaghighi")
            {
                SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                q.QueryType = SubSonic.QueryType.Select;
                q.OrderBy = tempQuery.OrderBy;

                q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhaghighi");
                DataSet dt = q.ExecuteDataSet();
                if (dt.Tables[0].Rows.Count > 0)
                {
                    GridViewUsers.DataSource = dt;
                    GridViewUsers.DataBind();
                }
                else
                {
                    Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                    cleargrid();
                }
            }
        }

        if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
        {
            SubSonic.Query q1 = DataAccess.TblUserDetail.CreateQuery();
            q1.QueryType = SubSonic.QueryType.Select;
            q1.OrderBy = tempQuery.OrderBy;
            
            q1.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
            q1.AND(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhoghooghi");
            q1.OR(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhaghighi");

            DataSet dt1 = q1.ExecuteDataSet();
            if (dt1.Tables[0].Rows.Count > 0)
            {
                GridViewUsers.DataSource = dt1;
                GridViewUsers.DataBind();
            }
            else
            {
                Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                cleargrid();
            }
        }
        if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
        {
            if (RadioButtonListSurveyor.SelectedItem.Value == "Advisorhoghooghi")
            {
                SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                q.QueryType = SubSonic.QueryType.Select;
                q.OrderBy = tempQuery.OrderBy;
                
                q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhoghooghi");
                DataSet dt = q.ExecuteDataSet();
                if (dt.Tables[0].Rows.Count > 0)
                {
                    GridViewUsers.DataSource = dt;
                    GridViewUsers.DataBind();
                }
                else
                {
                    Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                    cleargrid();
                }
            }
            if (RadioButtonListSurveyor.SelectedItem.Value == "Advisorhaghighi")
            {
                SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                q.QueryType = SubSonic.QueryType.Select;
                q.OrderBy = tempQuery.OrderBy;
                
                q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhaghighi");
                DataSet dt = q.ExecuteDataSet();
                if (dt.Tables[0].Rows.Count > 0)
                {
                    GridViewUsers.DataSource = dt;
                    GridViewUsers.DataBind();
                }
                else
                {
                    Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                    cleargrid();
                }
            }
        }
        if (RadioButtonListRole.SelectedItem.Value == "other")
        {
            SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
            q.QueryType = SubSonic.QueryType.Select;
            q.OrderBy = tempQuery.OrderBy;
            List<string> roles = new List<string>() { "Employer", "FourthFactor", "BonyadShahrestan", "EditUser", "ViewUser" };
            q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%")
                .AND(DataAccess.TblUserDetail.Columns.Role, Comparison.In, roles);
            DataSet dt = q.ExecuteDataSet();
            if (dt.Tables[0].Rows.Count > 0)
            {
                GridViewUsers.DataSource = dt;
                GridViewUsers.DataBind();
            }
            else
            {
                Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                cleargrid();
            }
        }
    }

    protected void RadioButtonListRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        {
            RadioButtonListAdvisors.Visible = true;
            RadioButtonListSurveyor.Visible = false;
        }
        else if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
        {
            RadioButtonListAdvisors.Visible = false;
            RadioButtonListSurveyor.Visible = true;
        }
        else
        {
          
            RadioButtonListSurveyor.Visible = false;
            RadioButtonListAdvisors.Visible = false;
        }
    }
    public void cleargrid()
    {
        GridViewUsers.DataSource = null;
        GridViewUsers.DataBind();
    }
    protected void GridViewUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         if(!e.CommandName.ToLower().Equals("Sort".ToLower()))
         {
                int id = Convert.ToInt32(e.CommandArgument);
                Guid userid = new Guid(GridViewUsers.DataKeys[id].Value.ToString());
                MembershipUser user = Membership.GetUser(userid);
                switch (e.CommandName)
                {
                    case "Deactive":

                        //string NameFamily = GridViewUsers.Rows[id].Cells[0].Text + " " + GridViewUsers.Rows[id].Cells[1].Text;
                        //Session["UserIdUpload"] = userid;

                        user.IsApproved = false;
                        Membership.UpdateUser(user);
                        cleargrid();
                        Lblstatus.Text = " <script>alert('.غیرفعال شدن کاربر با موفقیت انجام شد')</script>";
                        break;
                    case "Active":

                        //string NameFamily = GridViewUsers.Rows[id].Cells[0].Text + " " + GridViewUsers.Rows[id].Cells[1].Text;
                        //Session["UserIdUpload"] = userid;

                        user.IsApproved = true;
                        Membership.UpdateUser(user);
                        cleargrid();
                        Lblstatus.Text = " <script>alert('.فعال شدن کاربر با موفقیت انجام شد')</script>";

                        break;
                }
            }
    }

    protected void GridViewUsers_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes.Add("onclick", "return confirm('آيا از غیرفعال کردن کاربر مطمئن هستيد؟');");
            e.Row.Cells[1].Attributes.Add("onclick", "return confirm('آيا از فعال کردن کاربر مطمئن هستيد؟');");
          
         
        }
    }
    protected void GridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType== DataControlRowType.DataRow)
        {
            MembershipUser u = Membership.GetUser(e.Row.Cells[9].Text);
            if (u.IsApproved == true)
            {
                e.Row.Cells[9].ForeColor = System.Drawing.Color.Green;
                e.Row.Cells[9].Text = "کاربر فعال است.";
            }
            if (u.IsApproved == false)
            {
                e.Row.Cells[9].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[9].Text = "کاربر غیر فعال است.";
            }

            if (e.Row.Cells[8].Text == "Employer")
                e.Row.Cells[8].Text = "کارفرما";
            if (e.Row.Cells[8].Text == "BonyadShahrestan")
                e.Row.Cells[8].Text = "بنیاد شهرستان";
            if (e.Row.Cells[8].Text == "FourthFactor")
                e.Row.Cells[8].Text ="عامل چهار";
            if (e.Row.Cells[8].Text == "Advisorhoghooghi")
                e.Row.Cells[8].Text = "مشاور حقوقی";
            if (e.Row.Cells[8].Text == "Advisorhaghighi")
                e.Row.Cells[8].Text ="مشاور حقیقی";
            if (e.Row.Cells[8].Text == "Surveyor")
                e.Row.Cells[8].Text = "نقشه بردار";
            if (e.Row.Cells[8].Text == "Surveyorhoghooghi")
                e.Row.Cells[8].Text = " نقشه بردار حقوقی";
            if (e.Row.Cells[8].Text == "Surveyorhaghighi")
                e.Row.Cells[8].Text = " نقشه بردار حقیقی";
            if (e.Row.Cells[8].Text == "ViewUser")
                e.Row.Cells[8].Text = "کاربر عادی فقط مشاهده"; 
            if (e.Row.Cells[8].Text == "EditUser")
                e.Row.Cells[8].Text = "کاربر عادی ویرایش و ثبت";
        }
    }
    protected void GridViewUsers_OnSort(object sender, GridViewSortEventArgs e)
    {
        // get values from viewstate
        String sortExpression = ViewState["SortExpression"] as string;
        String sortDirection = ViewState["SortDirection"] as string;

        // on first time header clicked ( including different header), sort ASCENDING
        if (e.SortExpression != sortExpression)
        {
            sortExpression = e.SortExpression;
            sortDirection = "Ascending";
        }
        // on second time header clicked, toggle sort
        else
        {
            if (sortDirection == "Ascending")
            {
                sortExpression = e.SortExpression;
                sortDirection = "Descending";
            }
            // Descending
            else
            {
                sortExpression = e.SortExpression;
                sortDirection = "Ascending";
            }
        }

        // save state for next time

        ViewState["SortExpression"] = sortExpression;
        ViewState["SortDirection"] = sortDirection;
        BindingGrid();
    }

}